/*******************************************************************************
 * Dreampalace
 *******************************************************************************
 * 
 * Dreampalace is a special dreamdiary which helps dreamers to understand their
 * dreams. You can find also many features in Dreampalace to learn and hold the
 * lucid state in your dreams.
 * 
 * *****************************************************************************
 *
 * Program	   : Dreampalace
 * Autor           : PRo (Peter Rogge)
 * Email           : rogge-peter@gmx.de
 * Homepage        : http://code.google.com/p/dreampalace
 * License	   : GNU General Public License 3.0
 * 
 *******************************************************************************
 * 
 * This file is part of the project Dreampalace. Dreampalace is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation in version 3 of
 * the License only.
 * 
 * Dreampalace is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Dreampalace. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The Dreampalace project designates this particular file as subject to the
 * "Classpath" exception as provided by the Dreampalace project in the GPL 
 * Version 3 section of the License file that accompanied this code.
 * 
 *******************************************************************************
 */
package de.pro.dreampalace.database;

import de.pro.dreampalace.database.api.IConnectionManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Peter Rogge (PRo) <rogge-peter@gmx.de>
 */
@ServiceProvider(service=IConnectionManager.class)
public class ConnectionManager implements IConnectionManager {

    private Connection connection = null;
    
    @Override
    public Connection getConnection() {
        // Check if one connection is always created
        if (connection != null) { return connection; }
        
        // else create connection
        try {
            this.initConnection();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return connection;
    }
    
    @Override
    public void initConnection() throws ClassNotFoundException, SQLException {
        // Create connection to the database. If the database isn't created
        // then the db will automatically create
        Logger.getLogger(ConnectionManager.class.getName()).info(
                "Init org.hsqldb.jdbcDriver");
        Class.forName("org.hsqldb.jdbcDriver");

        final String msg = "Create connection to "
                + System.getProperty("user.dir")
                + "\\db\\DreampalaceDB";
        Logger.getLogger(ConnectionManager.class.getName()).info(msg);
        connection = DriverManager.getConnection(
                        "jdbc:hsqldb:file:" + System.getProperty("user.dir")
                        + "\\db\\DreampalaceDB", "sa", "");
    }

    @Override
    public void shutdownConnection() throws SQLException {
        // Check if the connection created or open
        if (connection == null || connection.isClosed()) { return; }
        
        // Close the connection to the database
        Logger.getLogger(DatabaseManager.class.getName()).info(
                "Shutdown the connection to the database.");
        
        final Statement st = connection.createStatement();
        st.execute("SHUTDOWN");
        st.close();
        connection.close();
        
        Logger.getLogger(DatabaseManager.class.getName()).info(
                "connection is closed.");
    }
}
